package qualomelhor.server.database;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import qualomelhor.database.*;
import qualomelhor.server.*;

import java.util.ArrayList;
import java.util.List;

public class ConfirmaUsuarioServlet extends HttpServlet {
	
	private static final long serialVersionUID = 0;
	
	    // Initialize a key set somehow:
    public void doGet(HttpServletRequest  req, HttpServletResponse res) throws IOException, ServletException {
    	doPost(req, res);
	}

	public void doPost(HttpServletRequest  req, HttpServletResponse res) throws IOException, ServletException {
		DAO d = new DAO();
		try {
			String email = req.getParameter("txtEmail");
			String uuid = req.getParameter("txtCodigoConfirm");
			System.out.println("email = " + email);
			System.out.println("uuid = " + uuid);

			Usuario u = d.getUsuario(email);
			if (u != null) {
				System.out.println("Usuario confirmado! Setando usu�rio confirmado!");
				if (u.getUuid().equals(uuid)) {
					u.setUsuarioConfirmado(true);
					d.gravaNovoUsuario(u);
					
					// Enviar email boas vindas para usuario aqui!!!
					
					System.out.println("Usu�rio atualizado para confirmado!");

					res.sendRedirect("index.jsp?pagina=usuarioConfirmado");
				} else {
					System.out.println("UUID n�o confere!");
					res.sendRedirect("erro.jsp");
				}
			} else {
				System.out.println("Email n�o encontrado!");
				res.sendRedirect("erro.jsp");
			}

		} catch(Exception e) {
	            // Show the submit page again, but with a bit more 
	            // intimidating output.
			e.printStackTrace();
		}
	}
}